namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorDifferentOfMovingAverage : BaseIndicator
    {
        private string firstMaField = ("DMA.FirstMaField" + Common.GetGuid());
        private string secondMaField = ("DMA.SecondMaField" + Common.GetGuid());
        private LineShape tlsAma;
        private LineShape tlsDma;

        public IndicatorDifferentOfMovingAverage()
        {
            base.IndType = IndicatorType.DMA;
            base.IndName = "DMA";
            base.IndDesc = "平行线差指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "SHORT", 10.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "LONG", 50.0));
            base.IndPeriod.Add(new IndicatorPeriod(2, "M", 10.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "DMA", "AMA" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.tlsDma) && (obj != this.tlsAma))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curValue = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num2 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[0].ParamValue, this.firstMaField, base.SourceDic["Close"], curValue, base.Crab.DataSource);
            double num3 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.secondMaField, base.SourceDic["Close"], curValue, base.Crab.DataSource);
            double num4 = num2 - num3;
            double num5 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[2].ParamValue, this.tlsAma.Name, this.tlsDma.Name, num4, base.Crab.DataSource);
            return new double[] { num4, num2, num3, num5 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsDma, this.tlsAma };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsDma);
            base.Crab.DeleteShape(this.TlsAma);
            base.Crab.DataSource.Columns.Remove(this.FirstMaField);
            base.Crab.DataSource.Columns.Remove(this.SecondMaField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.TlsDma.Name, numArray[0], r);
                        base.Crab.SetValue(this.FirstMaField, numArray[1], r);
                        base.Crab.SetValue(this.SecondMaField, numArray[2], r);
                        base.Crab.SetValue(this.TlsAma.Name, numArray[3], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsDma = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsAma = base.Crab.AddLine(fields[1], panelID);
            base.Crab.SetLine(fields[1], fields[1], Color.Yellow, 1, 0.01, 2, DashStyle.Solid);
            base.Crab.DataSource.Columns.Add(this.FirstMaField);
            base.Crab.DataSource.Columns.Add(this.SecondMaField);
            base.SourceDic["Close"] = source[0];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.IndPeriod[2].ParamValue = periods[2];
        }

        public string FirstMaField
        {
            get
            {
                return this.firstMaField;
            }
            set
            {
                this.firstMaField = value;
            }
        }

        public string SecondMaField
        {
            get
            {
                return this.secondMaField;
            }
            set
            {
                this.secondMaField = value;
            }
        }

        public LineShape TlsAma
        {
            get
            {
                return this.tlsAma;
            }
            set
            {
                this.tlsAma = value;
            }
        }

        public LineShape TlsDma
        {
            get
            {
                return this.tlsDma;
            }
            set
            {
                this.tlsDma = value;
            }
        }
    }
}

